﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;


    public class Common
    {
        public static XmlDocument doc;
        public static XmlNodeList nodeList;
        static FileInfo xmlfile = new FileInfo(System.Windows.Forms.Application.StartupPath + @"/config.xml");
        static Common(){

        }
        public static void setXmlDoc(string rootStr)
        {
            doc = new XmlDocument();
            doc.Load(xmlfile.FullName);
            nodeList = doc.SelectSingleNode(rootStr).ChildNodes;
        }

        public static bool saveSystem(string name, string path)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Name")
                    {
                        xe.SetAttribute("value", name);
                    }
                    if (xe.Name == "Path")
                    {
                        xe.SetAttribute("value", path);
                    }
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveDomain(string domain)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Domain")
                    {
                        xe.SetAttribute("value", domain);
                    }
                    
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveAPI(string api)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "API")
                    {
                        xe.SetAttribute("value", api);
                    }

                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveUserToken(string openid)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "UserToken")
                    {
                        xe.SetAttribute("value", openid);
                    }

                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static string GetConfig(string name)
        {
            try
            {
                string val="";
                Common.setXmlDoc("root//System");
                foreach (XmlNode node in Common.nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == name)
                    {
                        val = xe.GetAttribute("value");
                        break;
                    }
                }
                return val;

            }
            catch
            {
                return "";
            }
        }
        
    }

